package com.cskaoyan.service.wx.hx;


import com.cskaoyan.mapper.MarketGoodsMapper;
import com.cskaoyan.mapper.MarketKeywordMapper;
import com.cskaoyan.mapper.MarketSearchHistoryMapper;
import com.cskaoyan.module.bean.BaseRespVo;
import com.cskaoyan.module.bo.SearchData;
import com.cskaoyan.module.pojo.*;
import com.cskaoyan.module.vo.HistoryKeyword;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 处理查找方法的service实现类
 * 创建日期: 2022/04/10 21:47
 *
 * @author Administrator
 */
@Service
public class VXSearchServiceImpl implements VXSearchService {
    @Autowired
    MarketKeywordMapper marketKeyWordMapper;
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    MarketSearchHistoryMapper marketSearchHistoryMapper;



    @Override
    public BaseRespVo searchIndex() {
         //找出所有的关键字
        MarketKeywordExample example = new MarketKeywordExample();
        MarketKeywordExample.Criteria criteria = example.createCriteria();
        criteria.andIdIsNotNull();
        List<MarketKeyword> marketKeywords = marketKeyWordMapper.selectByExample(example);

        SearchData searchData = new SearchData();
        List<MarketKeyword> hotKeywordList = new ArrayList<>();
        // 查询到所有的historyKeyword，将其装入HistoryKeyword的数组中
        MarketSearchHistoryExample searchHistoryExample = new MarketSearchHistoryExample();
        searchHistoryExample.createCriteria().andIdIsNotNull();
        List<MarketSearchHistory> historyList = marketSearchHistoryMapper.selectByExample(searchHistoryExample);
        ArrayList<HistoryKeyword> historyKeywords = new ArrayList<>();

        for (MarketSearchHistory searchHistory : historyList) {
            HistoryKeyword historyKeyword = new HistoryKeyword();
            historyKeyword.setKeyword(searchHistory.getKeyword());
            historyKeywords.add(historyKeyword);
        }


        if (marketKeywords == null) {
            return null;
        }
        for (MarketKeyword marketKeyword : marketKeywords) {
            if (marketKeyword.getIsDefault()) {
                // 将Keyword是默认值的封装进DefaultKeyword中
                searchData.setDefaultKeyword(marketKeyword);

            } else {
                // 将Keyword是热度值的封装进hotKeywordList中
                hotKeywordList.add(marketKeyword);
            }

        }

        searchData.setHistoryKeywordList(historyKeywords);
        searchData.setHotKeywordList(hotKeywordList);

        return BaseRespVo.ok(searchData);
    }

    @Override
    public BaseRespVo searchHelper(String  keyword) {
        // 获取用户id
        Subject subject = SecurityUtils.getSubject();
        MarketUser marketUser = (MarketUser) subject.getPrincipals().getPrimaryPrincipal();
        Integer id = marketUser.getId();


        MarketGoodsExample example = new MarketGoodsExample();
        MarketGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andNameLike("%" + keyword + "%");

        if (keyword != null && !" ".equals(keyword)) {
            MarketSearchHistory searchHistory = new MarketSearchHistory();
            searchHistory.setAddTime(new Date());
            searchHistory.setKeyword(keyword);
            searchHistory.setUserId(id);
            marketSearchHistoryMapper.insertSelective(searchHistory);

            List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(example);
            ArrayList<String> names = new ArrayList<>();
            if (marketGoods != null) {
                names.add(keyword);
                for (MarketGoods marketGood : marketGoods) {
                    String name = marketGood.getName();
                    names.add(name);
                }
                return BaseRespVo.ok(names);
            }

        }
        return BaseRespVo.ok(null);
    }

    @Override
    public void clearhistory() {
        MarketSearchHistoryExample example = new MarketSearchHistoryExample();
        MarketSearchHistoryExample.Criteria criteria = example.createCriteria();
        criteria.andIdIsNotNull();
        marketSearchHistoryMapper.deleteByExample(example);
    }
}
